/**
 *CreateChessBoard.java
 * Dimiourgi to Board pou vlepi o hristis kai bori na hirizete me to mouse
 * 
 */

package GUI;

import GUI.MouseHandeling;
import gameStructure.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
 
public class CreateChessBoard extends JLabel{
    public SquareOnBoard[][] sqrs;
    
    /** Creates a new instance of CreateChessBoard */
    public CreateChessBoard(){
        setSize(800,700);
        setLayout(new GridLayout(8,8));
        sqrs = new SquareOnBoard[8][8];
        createSquares();
    }
    
    /**
     * 
     * @param h Diastasis: height.
     * @param w Diastasis: width.
     */
    public CreateChessBoard(int h, int w){
        setSize(h,w);
        setLayout(new GridLayout(8,8));
        sqrs = new SquareOnBoard[8][8];
        createSquares();
    }
    
    private void createSquares(){
        for(int i=0;i<8;i++){
            for(int j=0;j<8;j++){
                SquareOnBoard panel = new SquareOnBoard(""+i+""+j);
                panel.setSquareColor(getColor(i,j));
                panel.addMouseListener(new MouseHandeling());
                add(panel);
                sqrs[i][j] = panel;
            }
        }
    }
    
    
    private Color getColor(int x, int y){
        if((x+y)%2 == 0)
            return Color.WHITE;
        else
            return Color.DARK_GRAY;
    }
    
    /**
     * 
     * @param Board To Board pou periehi oles tis thesis pou ine pithanon na pai to Piece.
     */
    public void possibleMoves(Vector<Point> Board){
        createSquares();// Prota kani ta tetragona xana aspromaura se periptosi pou ehoun alaxi idi hroma.
        for(int i=0;i<Board.capacity();i++){
                SquareOnBoard panel = new SquareOnBoard(""+Board.get(i).x+""+Board.get(i).y);
                panel.setSquareColor(Color.GREEN);
                panel.addMouseListener(new MouseHandeling());
                add(panel);
                sqrs[Board.get(i).x][Board.get(i).y] = panel;
            }
    }
    
    
    /**
     * TODO : apo tin proigoumeni Piece, tha kratithoun: addPiece kai removePiece 
     * oste na topothetounde i ikones sta andistiha simia se andistihia me tin Board. 
     * @param Board O pinakas me oles ta Pieces kai tis thesis stis opies auta vriskonde
     * @return Epistefi to chessboard me ta "grafika" pieces epano
     * pragma pou ine doulia mono tou GUI.
     */
    public CreateChessBoard setPieces( ArrayList Board){
        CreateChessBoard t = new CreateChessBoard();
        
        Point pos= new Point();
        pos.x=0;
        pos.y=1;
        boolean mov = false;
        sqrs[pos.x][pos.y].addPiece(new paintPiece( mov, pos,'w', "Knight"));
        sqrGUI p3 = new paintPiece(!mov, pos, 'b', "Knight");
        p3.addMouseListener(new MouseHandeling());
        sqrs[7][1].addPiece(p3);
        
        return t;
    }
}